home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / english / dcodrle1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  3.3 KB  |  100 lines

  1. /* File: dcodrle1.c
  2.    Author: David Bourgin
  3.    Creation date: 1/2/94
  4.    Last update: 24/7/95
  5.    Purpose: Example of RLE type 1 decoding with a file source to decompress
  6. */
  7.  
  8. #include <stdio.h>
  9. /* For routines printf,fgetc,fputc and fwrite */
  10. #include <memory.h>
  11. /* For routine memset */
  12. #include <stdlib.h>
  13. /* For routine exit */
  14.  
  15. /* Error codes send to the caller */
  16. #define NO_ERROR      0
  17. #define BAD_FILE_NAME 1
  18. #define BAD_ARGUMENT  2
  19.  
  20. /* Useful constants */
  21. #define FALSE 0
  22. #define TRUE  1
  23.  
  24. /* Global variables */
  25. FILE *source_file,*dest_file;
  26.  
  27.                              /* Being that fgetc=EOF only after an access
  28.                                 then 'byte_stored_status' is 'TRUE' if a byte has been stored by 'fgetc'
  29.                                 or 'FALSE' if there's no valid byte not already read and not not handled in 'val_byte_stored' */
  30. int byte_stored_status=FALSE;
  31. int val_byte_stored;
  32.  
  33. /* Pseudo procedures */
  34. #define end_of_data()  (byte_stored_status?FALSE:!(byte_stored_status=((val_byte_stored=fgetc(source_file))!=EOF)))
  35. #define read_byte()  (byte_stored_status?byte_stored_status=FALSE,(unsigned char)val_byte_stored:(unsigned char)fgetc(source_file))
  36. #define write_byte(byte)  ((void)fputc((byte),dest_file))
  37. #define write_array(array,byte_nb_to_write)  ((void)fwrite((array),1,(byte_nb_to_write),dest_file))
  38. #define write_block(byte,time_nb)  { unsigned char array_to_write[129];\
  39.                                      (void)memset(array_to_write,(byte),(time_nb));\
  40.                                      write_array(array_to_write,(time_nb));\
  41.                                    }
  42.  
  43. void rle1decoding()
  44. /* Returned parameters: None
  45.    Action: Decompresses with RLE type 1 method all bytes read by the function read_byte
  46.    Erreurs: An input/output error could disturb the running of the program
  47. */
  48. { unsigned char header;
  49.   register unsigned char i;
  50.  
  51.   while (!end_of_data())
  52.         { header=read_byte();
  53.           switch (header & 128)
  54.           { case 0:for (i=0;i<=header;i++)
  55.                        write_byte(read_byte());
  56.                    break;
  57.             case 128:write_block(read_byte(),(header & 127)+2);
  58.           }
  59.         }
  60. }
  61.  
  62. void help()
  63. /* Returned parameters: None
  64.    Action: Displays the help of the program and then stops its running
  65.    Erreurs: None
  66. */
  67. { printf("This utility enables you to decompress a file by using RLE type 1 method\n");
  68.   printf("as given in 'La Video et Les Imprimantes sur PC'\n");
  69.   printf("\nUse: dcodrle1 source target\n");
  70.   printf("source: Name of the file to decompress\n");
  71.   printf("target: Name of the restored file\n");
  72. }
  73.  
  74. int main(argc,argv)
  75. /* Returned parameters: Returns an error code (0=None)
  76.    Action: Main procedure
  77.    Errors: Detected, handled and an error code is returned, if any
  78. */
  79. int argc;
  80. char *argv[];
  81. { if (argc!=3)
  82.      { help();
  83.        exit(BAD_ARGUMENT);
  84.      }
  85.   else if ((source_file=fopen(argv[1],"rb"))==NULL)
  86.           { help();
  87.             exit(BAD_FILE_NAME);
  88.           }
  89.        else if ((dest_file=fopen(argv[2],"wb"))==NULL)
  90.                { help();
  91.                  exit(BAD_FILE_NAME);
  92.                }
  93.             else { rle1decoding();
  94.                    fclose(source_file);
  95.                    fclose(dest_file);
  96.                  }
  97.   printf("Execution of dcodrle1 completed.\n");
  98.   return (NO_ERROR);
  99. }
  100.